package controller.front;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import util.ActionForward;

import action.Action;
import action.ItemCartAddAction;
import action.ItemCartListAction;
import action.ItemCartQtyDownAction;
import action.ItemCartQtyUpAction;
import action.ItemCartRemoveAction;
import action.ItemListAction;
import action.ItemViewAction;
//@WebServlet("*.do")
public class ItemFrontController extends HttpServlet {
	private static final long serialVersionUID = 1L;
  /*
   이전 예제들은 요청마다 각각의 서블릿이 요청을 처리하기 위해서 매핑되어 있었다.
   그리고 서블릿 안에서 doGet() 혹은 doPost() 메소드중 하나가 요청을 처리하기 위해서 실행되어지는 구조였다.
   하지만 해당 예제는 모든 요청을 하나의 서블릿(ItemFrontController)에서 처리하기 때문에 get, post 방식중
   어떤 방식으로 요청이 들어오더라도 모든 처리해 줄수 있어야만 한다
   ::
   doProcess() 메소드를 사용해야하는 이유
   */
	
	protected void doProcess(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String requestURI =request.getRequestURI();
		String contextPath = request.getContextPath();
		String command = requestURI.substring(contextPath.length());
		System.out.println(command);
		Action action = null;
		ActionForward forward = null;
		
		if(command.equals("/itemList.do")){
			action = new ItemListAction();
			
			try{
				forward=action.execute(request, response);
				
			}catch(Exception e){
				e.printStackTrace();
			}
		}else if(command.equals("/itemView.do")){
			action = new ItemViewAction();
			try{
				forward = action.execute(request, response);
			}catch(Exception e){
				e.printStackTrace();
			}
		}else if(command.equals("/itemCartAdd.do")){
			action = new ItemCartAddAction();
			try{
				forward = action.execute(request, response);
			}catch(Exception e){
				e.printStackTrace();
			}
		}else if(command.equals("/itemCartList.do")){
			action = new ItemCartListAction();
			try{
				forward = action.execute(request, response);
			}catch(Exception e){
				e.printStackTrace();
			}
		}else if(command.equals("/itemCartRemove.do")){
			action = new ItemCartRemoveAction();
			try{
				forward = action.execute(request, response);
			}catch(Exception e){
				e.printStackTrace();
			}
		}else if(command.equals("/itemCartQtyUp.do")){
			action = new ItemCartQtyUpAction();
			try{
				forward = action.execute(request, response);
			}catch(Exception e){
				e.printStackTrace();
			}
		}else if(command.equals("/itemCartQtyDown.do")){
			action = new ItemCartQtyDownAction();
			try{
				forward = action.execute(request, response);
			}catch(Exception e){
				e.printStackTrace();
			}
		}
		
		if(forward != null){
			if(forward.isRedirect()){
				response.sendRedirect(forward.getUrl());
			}else{
				RequestDispatcher rdp=request.getRequestDispatcher(forward.getUrl());
				rdp.forward(request, response);
			}
		}
	}
		
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doProcess(request,response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doProcess(request,response);
	}

}











